#### FOR FIGURE S5 IN THE SUPPLEMENTARY INFORMATION:

rm(list=ls())
library(foreign)
library(ggplot2) 
library(gridExtra)
library(knitr)
library(grid)
library(dplyr)
library(forcats)
#install.packages("extrafont")
library(extrafont)
#install.packages("performance")
library("performance")
library("see")
library("haven")

######### Binary Treatment -------
setwd("~/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates")
setwd("/Users/sergimartinez/Dropbox/EUI/BET HOUSES PROJECT/data/estimates_050121/")

####### Setting the theme:

theme_results <- theme_bw(base_size = 26) +
  theme(text=element_text(family="Times",
                          size = 26),
        axis.text.x = element_text(size = 26, color = "black"),
        axis.text.y = element_text(size = 15, color = "black"),
        axis.title.y = element_text(size = 26, color = "black"),
        axis.title.x = element_text(size = 15, color = "black"),
        legend.position = "top",
        legend.key.size = unit(1.2, "cm"),
        legend.text = element_text(size = 26),
        legend.title = element_text(size = 26),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        strip.text.y.right = element_text(angle = 0, size = 26))

all <- read_dta("pau2.dta")

all$id2 <- factor(all$inc,
                  levels = c(0,1,2),
                  labels = c("Below average","All", "Above average"))

all$tipo <- factor(all$out,
                   levels = c(0,1),
                   labels = c("Outcome: Average grade (0-10)", "Outcome: % students passing the test"))

all$y2 <- factor(all$charter,
                 levels = c(0,1,2),
                 labels = c("Only charter \nhigh-schools",
                            "Only public \nhigh-schools",
                            "Overall average"))

library(dplyr)

all$charter2 <- all$charter + 1
all$charter2[all$charter2>2] <- 0

all$y2 <- factor(all$charter2,
                 levels = c(0,1,2),
                 labels = c("Overall average",
                            "Only charter \nhigh-schools",
                            "Only public \nhigh-schools"))


all$year <- factor(all$Year,
                   levels = c(0,1,2,3),
                   labels = c("Placebo t-2", "Placebo, t-1", "Effect for the\n Opening Year", "Effect on the First\nFull-Open Course"))


palette2 = c("black", "blue", "red")
all$year <- factor(all$Year,
                   levels = c(0,1,2,3),
                   labels = c('Placebo~t[-2]','Placebo~t[-1]','OpeningYear~t[0]', 'FirstFullYear~t[+1]'))

all$y2 <- factor(all$charter2,
                 levels = c(0,1,2),
                 labels = c("Overall\naverage",
                            "Charter\nschools",
                            "Public\nschools"))
figureS5 <- all[all$out==0 & all$Year>0,] %>%
  ggplot(aes(x = y2, y = coef, colour = id2)) +   
  geom_point(size = 2 ,position = position_dodge(.5)) +
  geom_errorbar(aes(ymax = ci_upper95, ymin = ci_lower95), width = 0,  size = .7, position = position_dodge(width = .5)) +
  geom_errorbar(aes(ymax = ci_upper90, ymin = ci_lower90), width = 0, size = 1.6, position = position_dodge(width = .5)) +
  scale_color_manual(name = "Neighborhood\nby income levels:",values = palette2)+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black") + 
  xlab("") +
  ylab("Effect of new betting houses opening at less\nthan 500m. on educational performance") +
  theme_results + 
  facet_wrap(year~., scales="free_x", labeller=label_parsed,nrow=1)+
  theme(strip.background =element_rect(fill="white"),
        axis.title.y = element_text(size = 26, color = "black"))
figureS5
#ggsave("050121_results_all_v3_120321.png", plot = figureS5, width = 40, height = 25, units = "cm")
